package com.elinkway.infinitemovies.http.parser;

import android.text.TextUtils;

import com.elinkway.infinitemovies.bean.LaunchImageData;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 启动图解析
 * <p/>
 * Created by wangyemin on 2015/11/5.
 */
public class LaunchImageDataParser extends LVideoMobileParser<LaunchImageData> {
	public static String STATUS_CODE = "200";
	public static String STATUS = "status";
	public static String CITY = "city";
	public static String START_PHOTOS = "startPhotos";
	public static String DEFAULT = "default";
	public static String EXTEND = "extend";
	public static String EXIT = "exit";
	public static String ANDROID = "480*800";
	public static String TIME = "time";
	public static String TYPE = "type";
	// wuxinrong added begin
	public static String DOWNLOAD = "download";
	public static String DOWNLOAD_SWITCH = "switch";
	// wuxinrong added end
	public static String CONFIG = "config";
	public static String CONFIG_REPORT = "report";

	// public static String PLATTYPE = "platType";
	// public static String CITYS_BLACK = "citiesBlack";
	// public static String CITYS_WHITE = "citiesWhite";

	@Override
	public LaunchImageData parse(JSONObject data) throws Exception {
		LaunchImageData launchData = new LaunchImageData();
		if (data != null && data.has(STATUS)
				&& data.optString(STATUS).equalsIgnoreCase(STATUS_CODE)) {
			launchData.setCity(data.optString(CITY));
			if (data.has(START_PHOTOS)) {
				JSONObject dataObject = data.optJSONObject(START_PHOTOS);
				if (dataObject.has(DEFAULT)) {
					JSONObject defaultObject = dataObject
							.optJSONObject(DEFAULT);
					if (defaultObject.has(ANDROID)) {
						JSONArray imageArr = defaultObject
								.getJSONArray(ANDROID);
						if (imageArr != null && imageArr.length() > 0)
							launchData.setDefaultImage(imageArr.getString(0));
					}
				}
				if (dataObject.has(EXTEND)) {
					JSONObject extendObject = dataObject.optJSONObject(EXTEND);
					if (extendObject.has(ANDROID)) {
						JSONArray adData = extendObject.getJSONArray(ANDROID);
						if (adData != null && adData.length() == 2) {
							launchData.setExtendImage(adData.getString(0));
							launchData.setAdUrl(adData.getString(1));
						}
					}
					if (extendObject.has(TIME)) {
						launchData.setAdTime(extendObject.optString(TIME));
					}
					if (extendObject.has(TYPE)) {
						if (TextUtils.isEmpty(extendObject.optString(TYPE))) {
							launchData.setAdType(1);
						} else{
							launchData.setAdType(Integer.valueOf(extendObject
									.optString(TYPE)));
						}
					}
					launchData.setExtendAdId(extendObject.optString("adId"));
				}
				//退出广告
				if (dataObject.has(EXIT)) {
					JSONObject exitObject = dataObject.optJSONObject(EXIT);
					if (exitObject.has(ANDROID)) {
						JSONArray adData = exitObject.getJSONArray(ANDROID);
						if (adData != null && adData.length() == 2) {
							launchData.setExitAdImage(adData.getString(0));
							launchData.setExitAdUrl(adData.getString(1));
						}
					}
					if (exitObject.has(TYPE)) {
						if (TextUtils.isEmpty(exitObject.optString(TYPE))) {
							launchData.setExitAdType("1");
						} else
							launchData.setExitAdType(exitObject
									.optString(TYPE));
					}
					launchData.setExitAdId(exitObject.optString("adId"));
				}
			}

            // wuxinrong added begin
            if (data.has(DOWNLOAD)) {
                JSONObject obj = data.optJSONObject(DOWNLOAD);
                if (obj.has(DOWNLOAD_SWITCH)) {
                    String switcher = obj.optString(DOWNLOAD_SWITCH);
                    launchData.setLiteAppSwitch(switcher);
                }
            }
            // wuxinrong added end

			if (data.has(CONFIG)) {
				JSONObject jsonObject = data.getJSONObject(CONFIG);
				if (jsonObject.has(CONFIG_REPORT)) {
					String report = jsonObject.optString(CONFIG_REPORT);
					launchData.setReport(report);
				}

			}


		}
		return launchData;
	}
}
